package com.zzj.portalwebsite.Controller;

import com.alibaba.fastjson.JSON;
import com.zzj.portalwebsite.Dao.Impl.ArticalInfoDaoImpl;
import com.zzj.portalwebsite.entity.article;
import com.zzj.portalwebsite.utlis.CommonResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/news/titles")
public class TitlesController extends HttpServlet {
    private ArticalInfoDaoImpl articalInfoDao = new ArticalInfoDaoImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String pagenum = request.getParameter("pagenum");
        if (pagenum == null){
            pagenum = "1";
        }

        List<article> getarticles = articalInfoDao.getarticles(Integer.parseInt(pagenum));
        getarticles.forEach(article -> {
//            System.out.println(article);
        });

        if (getarticles != null){
            response.getWriter().write(JSON.toJSONString(CommonResult.success(getarticles)));
        }else{
            response.getWriter().write("获取失败");
        }


    }


}
